package com.rxyb.hongyu.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Hex;

import it.sauronsoftware.base64.Base64;

public class HongyuSignUtils {

	private String keyChain = "a00000554d3da3";

	public HongyuSignUtils(String keyChain) {
		this.keyChain = keyChain;
	}

	public String getKeyChain() {
		return keyChain;
	}

	public void setKeyChain(String keyChain) {
		this.keyChain = keyChain;
	}

	public String testEn(String content) {
		try {
			content = URLEncoder.encode(content, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		content = Base64.encode(content);
		return content;
	}

	public String testDe(String encrypt) {
		encrypt = Base64.decode(encrypt);
		try {
			encrypt = URLDecoder.decode(encrypt, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return encrypt;
	}

	public String encrypt(String content) {
		String ret = "";
		try {
			content = URLEncoder.encode(content, "UTF-8");
			content = Base64.encode(content);
			// 生成key
			KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
			// keyGenerator.init(128, new SecureRandom(keyChain.getBytes()));
			SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
			random.setSeed(keyChain.getBytes());
			keyGenerator.init(128, random);

			SecretKey secretKey = keyGenerator.generateKey();
			byte[] key1 = secretKey.getEncoded();

			// key转换为密�?
			Key key2 = new SecretKeySpec(key1, "AES");
			// 加密
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5padding");
			cipher.init(Cipher.ENCRYPT_MODE, key2);
			byte[] result = cipher.doFinal(content.getBytes("UTF-8"));
			ret = Hex.encodeHexString(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	public String decrypt(String encrypt) {
		// String[] ret = new String[2];
		// ret[0] = "";
		// ret[1] = "";
		try {
			// 生成key
			KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
			// keyGenerator.init(128, new SecureRandom(keyChain.getBytes()));
			SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
			random.setSeed(keyChain.getBytes());
			keyGenerator.init(128, random);

			SecretKey secretKey = keyGenerator.generateKey();
			byte[] key1 = secretKey.getEncoded();
			// key转换为密�?
			Key key2 = new SecretKeySpec(key1, "AES");
			// 解密
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5padding");
			cipher.init(Cipher.DECRYPT_MODE, key2);
			byte[] result = cipher.doFinal(Hex.decodeHex(encrypt.toCharArray()));
			// ret[0] = "1";
			// ret[1] = URLDecoder.decode(Base64.decode(new String(result, "UTF-8")), "UTF-8");
			return URLDecoder.decode(Base64.decode(new String(result, "UTF-8")), "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
			// ret[0] = "-1";
			// ret[1] = e.getMessage().toString();
			return null;
		}
		// return ret;
	}

	public static void main(String[] args) {
		String reqDoc = "asdf";
		System.out.println("content:" + reqDoc);
		HongyuSignUtils aesUtil = new HongyuSignUtils("");
		String encrypt = aesUtil.encrypt(reqDoc);
		System.out.println("加密" + encrypt);
		String decrypt = aesUtil.decrypt(encrypt);
		System.out.println("解密" + decrypt);
	}

}
